<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="$t('sellerManage.编辑商户')">
      <template #right>
        <el-button type="primary" @click="goBack">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4"></SvgIcon
          >{{ $t('sellerManage.detail.返回上级页面') }}</el-button
        >
      </template>
    </PageHeader>
    <div class="mt24">
      <el-form
        :model="tenantInfo"
        :rules="rules"
        ref="formRef"
        label-width="0"
        :disabled="loading"
      >
        <div class="custom-table" style="width: 100%">
          <div class="titleRow">{{ $t('sellerManage.detail.店铺信息') }}</div>
          <div class="flex">
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.detail.店铺名称') }}
              </div>
              <div class="custom-table-value">
                <el-form-item :prop="'name' + locale">
                  <el-input
                    v-model="tenantInfo['name' + locale]"
                    :placeholder="$t('sellerManage.detail.请输入店铺名称')"
                  />
                </el-form-item>
              </div>
            </div>
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.入驻类型') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="settleType">
                  <el-radio-group v-model="tenantInfo.settleType">
                    <el-radio :label="1">{{ $t('sellerManage.edit.个人入驻') }}</el-radio>
                    <el-radio :label="2">{{ $t('sellerManage.edit.企业入驻') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="flex">
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.开店时长') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="ruleId">
                  <el-select
                    v-model="tenantInfo.ruleId"
                    :placeholder="$t('sellerManage.edit.请选择开店时长')"
                  >
                    <el-option
                      v-for="item in ruleList"
                      :key="item.id"
                      :label="`${item.duration}年 ${item.fee}元`"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </div>
            </div>
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.经营类型') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="skuClassifyIds">
                  <div class="overflow-y-auto w-full" style="max-height: 300px">
                    <el-tree
                      ref="treeRef"
                      :data="businessTypeOptions"
                      show-checkbox
                      node-key="id"
                      :props="{ label: 'name' + locale }"
                      @check="handleCheckChange"
                      :key="treeKey"
                    />
                  </div>
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="titleRow">{{ $t('sellerManage.edit.店铺联系人信息') }}</div>
          <template v-if="tenantInfo.settleType === 1">
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.所在地') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleLocation">
                    <el-input
                      v-model="tenantInfo.settleLocation"
                      :placeholder="$t('sellerManage.edit.请输入所在地')"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.详细地址') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleAddress">
                    <el-input
                      v-model="tenantInfo.settleAddress"
                      :placeholder="$t('sellerManage.edit.请输入详细地址')"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.联系人') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleContact">
                    <el-input
                      v-model="tenantInfo.settleContact"
                      :placeholder="$t('sellerManage.edit.请输入联系人')"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.联系手机号') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settlePhone" :rules="phoneRules">
                    <el-input
                      v-model="tenantInfo.settlePhone"
                      :placeholder="$t('sellerManage.edit.请输入联系手机号')"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.身份证正面') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleIdImage1">
                    <UploadFile
                      v-model="identityImages.front"
                      :limit="1"
                      :file-size="5"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.身份证反面') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleIdImage2">
                    <UploadFile
                      v-model="identityImages.back"
                      :limit="1"
                      :file-size="5"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
          </template>
          <template v-if="tenantInfo.settleType === 2">
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.公司名称') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleCompanyName">
                    <el-input
                      v-model="tenantInfo.settleCompanyName"
                      :placeholder="$t('sellerManage.edit.请输入公司名称')"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.所在地') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleLocation">
                    <el-input
                      v-model="tenantInfo.settleLocation"
                      :placeholder="$t('sellerManage.edit.请输入所在地')"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.详细地址') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleAddress">
                    <el-input
                      v-model="tenantInfo.settleAddress"
                      :placeholder="$t('sellerManage.edit.请输入详细地址')"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.联系人') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleContact">
                    <el-input
                      v-model="tenantInfo.settleContact"
                      :placeholder="$t('sellerManage.edit.请输入联系人')"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            <div class="custom-table-row">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.联系手机号') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="settlePhone" :rules="phoneRules">
                  <el-input
                    v-model="tenantInfo.settlePhone"
                    :placeholder="$t('sellerManage.edit.请输入联系手机号')"
                  />
                </el-form-item>
              </div>
            </div>
            <div class="titleRow">{{ $t('sellerManage.edit.营业执照信息') }}</div>
            <div class="custom-table-row">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.营业执照') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="settleLicenseImage">
                  <UploadFile
                    v-model="companyImages.license"
                    :limit="1"
                    :file-size="5"
                  />
                </el-form-item>
              </div>
            </div>
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.法人身份证正面') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleIdImage1">
                    <UploadFile
                      v-model="identityImages.front"
                      :limit="1"
                      :file-size="5"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.法人身份证反面') }}
                </div>
                <div class="custom-table-value">
                  <el-form-item prop="settleIdImage2">
                    <UploadFile
                      v-model="identityImages.back"
                      :limit="1"
                      :file-size="5"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            <div class="flex">
              <div class="custom-table-row flex1">
                <div class="custom-table-label" style="width: 200px">
                  {{ $t('sellerManage.edit.补充认证一') }}
                </div>
                <div class="custom-table-value">
                  <UploadFile
                    v-model="extraImages.extra1"
                    :limit="1"
                    :file-size="5"
                  />
                </div>
              </div>
              <div class="custom-table-row flex1">
                <div class="custom-table-label" style="width: 200px">
                  {{ $t('sellerManage.edit.补充认证二') }}
                </div>
                <div class="custom-table-value">
                  <UploadFile
                    v-model="extraImages.extra2"
                    :limit="1"
                    :file-size="5"
                  />
                </div>
              </div>
            </div>
            <div class="custom-table-row">
              <div class="custom-table-label" style="width: 200px">
                {{ $t('sellerManage.edit.补充认证三') }}
              </div>
              <div class="custom-table-value">
                <UploadFile
                  v-model="extraImages.extra3"
                  :limit="1"
                  :file-size="5"
                />
              </div>
            </div>
          </template>

          <div class="titleRow">{{ $t('sellerManage.edit.佣金比例及收款信息') }}</div>
          <div class="flex">
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.佣金比例') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="commissionRate">
                  <div class="flex_aic">
                    <el-input-number
                      v-model="tenantInfo.commissionRate"
                      :min="0"
                      :max="100"
                      :step="1"
                    />
                    <span class="ml8">%</span>
                  </div>
                </el-form-item>
              </div>
            </div>
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                  {{ $t('sellerManage.edit.收款人姓名') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="accountBankAccountName">
                  <el-input
                    v-model="configData.accountBankAccountName"
                      :placeholder="$t('sellerManage.edit.请输入收款人姓名')"
                  />
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="flex">
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.收款账户号码') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="accountBankAccountNo">
                  <el-input
                    v-model="configData.accountBankAccountNo"
                    :placeholder="$t('sellerManage.edit.请输入收款账户号码')"
                  />
                </el-form-item>
              </div>
            </div>
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.收款银行名称') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="accountBankName">
                  <el-input
                    v-model="configData.accountBankName"
                    :placeholder="$t('sellerManage.edit.请输入收款银行名称')"
                  />
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="flex">
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.收款银行地址') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="accountBankAddress">
                  <el-input
                    v-model="configData.accountBankAddress"
                    :placeholder="$t('sellerManage.edit.请输入收款银行地址')"
                  />
                </el-form-item>
              </div>
            </div>
            <div class="custom-table-row flex1">
              <div class="custom-table-label required" style="width: 200px">
                {{ $t('sellerManage.edit.SWIFT CODE') }}
              </div>
              <div class="custom-table-value">
                <el-form-item prop="accountBankCode">
                  <el-input
                    v-model="configData.accountBankCode"
                    :placeholder="$t('sellerManage.edit.请输入SWIFT CODE')"
                  />
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="custom-table-row">
            <div class="custom-table-label required" style="width: 200px">
              {{ $t('sellerManage.edit.支付凭证') }}
            </div>
            <div class="custom-table-value">
              <el-form-item prop="payImage">
                <UploadFile
                  v-model="paymentImages"
                  :limit="1"
                  :file-size="5"
                  :disabled="true"
                />
              </el-form-item>
            </div>
          </div>
        </div>
      </el-form>
    </div>

    <div class="mt24 flex_aic_jcc">
      <el-button @click="goBack">{{ $t('sellerManage.edit.返回') }}</el-button>
      <el-button type="primary" @click="handleSave" :loading="loading"
        >{{ $t('sellerManage.edit.保存') }}</el-button
      >
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, computed } from "vue";
import Api from "@/config/api/api";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import UploadFile from "@/components/UploadFile.vue";
import { getLangs } from "@/i18n/tools";

const { locale, t: $t } = useI18n();
const route = useRoute();
const router = useRouter();
const tenantInfo = ref({
  settleType: 1, // 默认为个人入驻
  commissionRate: 0, // 默认佣金比例
});
const configData = ref({});
const loading = ref(false);
const formRef = ref(null);
const ruleList = ref([]);
const businessTypeOptions = ref([]);
const treeRef = ref(null);
const treeKey = ref(0);
// 手机号验证规则
const phoneRules = [
  { required: true, message: $t('sellerManage.edit.请输入联系手机号'), trigger: "blur" },
  {
    pattern: /^1[3-9]\d{9}$/,
    message: $t('sellerManage.edit.请输入正确的手机号格式'),
    trigger: "blur",
  },
];

// 表单验证规则
const rules = ref({
  // 店铺基本信息
  ["name" + locale.value]: [
    { required: true, message: $t('sellerManage.edit.请输入店铺名称'), trigger: "blur" },
  ],
  settleType: [
    { required: true, message: $t('sellerManage.edit.请选择入驻类型'), trigger: "change" },
  ],

  // 联系人信息
  settleLocation: [
    { required: true, message: $t('sellerManage.edit.请输入所在地'), trigger: "blur" },
  ],
  settleAddress: [
    { required: true, message: $t('sellerManage.edit.请输入详细地址'), trigger: "blur" },
  ],
  settleContact: [{ required: true, message: $t('sellerManage.edit.请输入联系人'), trigger: "blur" }],
  settlePhone: [
    { required: true, message: $t('sellerManage.edit.请输入联系手机号'), trigger: "blur" },
  ],

  // 企业入驻特有信息
  settleCompanyName: [
    { required: true, message: $t('sellerManage.edit.请输入公司名称'), trigger: "blur" },
  ],

  // 证件图片
  settleIdImage1: [
    { required: true, message: $t('sellerManage.edit.请上传身份证正面照片'), trigger: "change" },
  ],
  settleIdImage2: [
    { required: true, message: $t('sellerManage.edit.请上传身份证反面照片'), trigger: "change" },
  ],
  settleLicenseImage: [
    { required: true, message: $t('sellerManage.edit.请上传营业执照'), trigger: "change" },
  ],

  // 佣金和收款信息
  commissionRate: [
    { required: true, message: $t('sellerManage.edit.请设置佣金比例'), trigger: "change" },
  ],
  payImage: [{ required: true, message: $t('sellerManage.edit.请上传支付凭证'), trigger: "change" }],
  ruleId: [{ required: true, message: $t('sellerManage.edit.请选择开店时长'), trigger: "change" }],
  skuClassifyIds: [
    { required: true, message: $t('sellerManage.edit.请选择经营类型'), trigger: "change" },
  ],
});

// 图片对象，用于UploadFile组件
const identityImages = ref({
  front: [],
  back: [],
});

const companyImages = ref({
  license: [],
});

const extraImages = ref({
  extra1: [],
  extra2: [],
  extra3: [],
});

const paymentImages = ref([]);

// 同步图片数据到表单
watch(
  identityImages,
  (newVal) => {
    if (newVal.front && newVal.front.length > 0) {
      tenantInfo.value.settleIdImage1 = newVal.front[0].url || "";
    } else {
      tenantInfo.value.settleIdImage1 = "";
    }
    if (newVal.back && newVal.back.length > 0) {
      tenantInfo.value.settleIdImage2 = newVal.back[0].url || "";
    } else {
      tenantInfo.value.settleIdImage2 = "";
    }
  },
  { deep: true }
);

watch(
  companyImages,
  (newVal) => {
    if (newVal.license && newVal.license.length > 0) {
      tenantInfo.value.settleLicenseImage = newVal.license[0].url || "";
    } else {
      tenantInfo.value.settleLicenseImage = "";
    }
  },
  { deep: true }
);

watch(
  extraImages,
  (newVal) => {
    if (newVal.extra1 && newVal.extra1.length > 0) {
      tenantInfo.value.settleExtra1 = newVal.extra1[0].url || "";
    } else {
      tenantInfo.value.settleExtra1 = "";
    }
    if (newVal.extra2 && newVal.extra2.length > 0) {
      tenantInfo.value.settleExtra2 = newVal.extra2[0].url || "";
    } else {
      tenantInfo.value.settleExtra2 = "";
    }
    if (newVal.extra3 && newVal.extra3.length > 0) {
      tenantInfo.value.settleExtra3 = newVal.extra3[0].url || "";
    } else {
      tenantInfo.value.settleExtra3 = "";
    }
  },
  { deep: true }
);

watch(
  paymentImages,
  (newVal) => {
    if (newVal && newVal.length > 0) {
      tenantInfo.value.payImage = newVal[0].url || "";
    } else {
      tenantInfo.value.payImage = "";
    }
  },
  { deep: true }
);

// 监听入驻类型变化，动态调整验证规则
watch(
  () => tenantInfo.value.settleType,
  (newVal) => {
    if (newVal === 1) {
      // 个人入驻不需要验证公司相关字段
      delete rules.value.settleCompanyName;
      delete rules.value.settleLicenseImage;
    } else {
      // 企业入驻需要验证公司相关字段
      rules.value.settleCompanyName = [
        { required: true, message: $t('sellerManage.edit.请输入公司名称'), trigger: "blur" },
      ];
      rules.value.settleLicenseImage = [
        { required: true, message: $t('sellerManage.edit.请上传营业执照'), trigger: "change" },
      ];
    }
  }
);

watch(
  () => locale.value,
  () => {
    treeKey.value++;
  }
);

// 获取商户信息
const getTenantInfo = async () => {
  try {
    loading.value = true;
    const res = await Api.tenantInfo(route.query.id);
    if (res.status === 200) {
      tenantInfo.value = res.data || {};
      tenantInfo.value.skuClassifyIds = getSkuClassifyIds();
      console.log(
        "tenantInfo.value.skuClassifyIds===",
        tenantInfo.value.skuClassifyIds
      );
      treeRef.value.setCheckedKeys(tenantInfo.value.skuClassifyIds);
      // 初始化图片数据
      initImageData();
    }
  } catch (error) {
    console.error("获取商户信息失败", error);
    ElMessage.error($t('sellerManage.edit.获取商户信息失败'));
  } finally {
    loading.value = false;
  }
};

// 初始化图片数据
const initImageData = () => {
  // 身份证图片
  if (tenantInfo.value.settleIdImage1) {
    identityImages.value.front = [
      { url: tenantInfo.value.settleIdImage1, name: $t('sellerManage.edit.身份证正面') },
    ];
  }
  if (tenantInfo.value.settleIdImage2) {
    identityImages.value.back = [
      { url: tenantInfo.value.settleIdImage2, name: $t('sellerManage.edit.身份证反面') },
    ];
  }

  // 营业执照
  if (tenantInfo.value.settleLicenseImage) {
    companyImages.value.license = [
      { url: tenantInfo.value.settleLicenseImage, name: $t('sellerManage.edit.营业执照') },
    ];
  }

  // 补充图片
  if (tenantInfo.value.settleExtra1) {
    extraImages.value.extra1 = [
      { url: tenantInfo.value.settleExtra1, name: $t('sellerManage.edit.补充认证一') },
    ];
  }
  if (tenantInfo.value.settleExtra2) {
    extraImages.value.extra2 = [
      { url: tenantInfo.value.settleExtra2, name: $t('sellerManage.edit.补充认证二') },
    ];
  }
  if (tenantInfo.value.settleExtra3) {
    extraImages.value.extra3 = [
      { url: tenantInfo.value.settleExtra3, name: $t('sellerManage.edit.补充认证三') },
    ];
  }

  // 支付凭证
  if (tenantInfo.value.payImage) {
    paymentImages.value = [
      { url: tenantInfo.value.payImage, name: $t('sellerManage.edit.支付凭证') },
    ];
  }
};

// 获取平台配置
const getPlatformConfig = async () => {
  try {
    const res = await Api.getConfig();
    if (res.status === 200) {
      configData.value = res.data || {};
    }
  } catch (error) {
    console.error("获取平台配置失败", error);
    ElMessage.error($t('sellerManage.edit.获取平台配置失败'));
  }
};

const getParams = () => {
  const defauleParams = {
    id: tenantInfo.value.id,
    settleType: 1,
    commissionRate: 0,
    payImage: tenantInfo.value.payImage,
    settleLocation: tenantInfo.value.settleLocation,
    settleAddress: tenantInfo.value.settleAddress,
    settleContact: tenantInfo.value.settleContact,
    settlePhone: tenantInfo.value.settlePhone,
    settleIdImage1: tenantInfo.value.settleIdImage1,
    settleIdImage2: tenantInfo.value.settleIdImage2,
    ruleId: tenantInfo.value.ruleId,
  };
  getLangs().forEach((lang) => {
    defauleParams[`name${lang}`] = tenantInfo.value[`name${lang}`] || "";
  });

  const params = {};
  if (tenantInfo.value.settleType === 2) {
    params.settleCompanyName = tenantInfo.value.settleCompanyName;
    params.settleAddress = tenantInfo.value.settleAddress;
    params.settleContact = tenantInfo.value.settleContact;
    params.settlePhone = tenantInfo.value.settlePhone;
    params.settleLicenseImage = tenantInfo.value.settleLicenseImage;
    params.settleIdImage1 = tenantInfo.value.settleIdImage1;
    params.settleIdImage2 = tenantInfo.value.settleIdImage2;

    params.settleExtra1 = tenantInfo.value.settleExtra1;
    params.settleExtra2 = tenantInfo.value.settleExtra2;
    params.settleExtra3 = tenantInfo.value.settleExtra3;
  }
  params.skuClassifyIds = tenantInfo.value.skuClassifyIds.filter(
    (v) => !businessTypeOptions.value.find((item) => item.id == v)
  );

  return {
    ...defauleParams,
    ...params,
  };
};

// 保存商户信息
const handleSave = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) {
      ElMessage.warning($t('sellerManage.edit.请填写完整的表单信息'));
      return;
    }
    try {
      loading.value = true;
      // 保存商户信息
      const res = await Api.updateCommissionRate(getParams());
      if (res.status === 200) {
        ElMessage.success($t('sellerManage.edit.保存成功'));
        // 返回上一页
        router.back();
      } else {
        ElMessage.error(res.message || $t('sellerManage.edit.保存失败'));
      }
    } finally {
      loading.value = false;
    }
  });
};

// 返回上一页
const goBack = () => {
  router.back();
};
// 监听树形结构选择变化
const handleCheckChange = () => {
  if (!treeRef.value) return;
  const checkedKeys = treeRef.value.getCheckedKeys();
  console.log("checkedKeys===", checkedKeys);

  tenantInfo.value.skuClassifyIds = checkedKeys;
};
const getRuleList = async () => {
  const res = await Api.tenantRenewRule({ page: 0, size: 1000 });
  ruleList.value = res?.data?.content || [];
};

// 获取经营类目
const getBusinessCategory = async () => {
  const res = await Api.businessCategory();
  businessTypeOptions.value = res?.data || [];
};

const getSkuClassifyIds = () => {
  const ids = [];
  tenantInfo.value.skuClassifys.map((v) => {
    ids.push(v.skuClassifyId);
    ids.push(v.skuClassifyParentId);
  });
  return [...new Set(ids)];
};
onMounted(async () => {
  await getBusinessCategory();
  getRuleList();
  getTenantInfo();
  getPlatformConfig();
});
</script>

<style lang="scss" scoped>
.reasonBox {
  width: 650px;
  height: 100px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 10px;
  font-size: 14px;
  color: #333;
}

.titleRow {
  height: 36px;
  line-height: 36px;
  background: rgba(255, 120, 45, 0.05);
  border-radius: 3px;
  padding-left: 10px;
  margin-top: 12px;
  font-weight: 500;
  color: #333;
}
</style>
